package com.example.etwzgzxt.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;

import com.example.etwzgzxt.R;
import com.example.etwzgzxt.tool.HttpHelper;
import com.example.etwzgzxt.tool.InfoApplication;
import com.example.etwzgzxt.tool.tool;
import com.example.etwzgzxt.view.activity.MainActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cz.msebera.android.httpclient.NameValuePair;
import cz.msebera.android.httpclient.message.BasicNameValuePair;

/**
 * Created by liyeting on 2016/5/14 0014.
 */
public class PostTask extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return myBinder;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Timer timer = new Timer();
        timer.schedule(new Work(), 0, 24000);

    }

    class Work extends TimerTask {

        @Override
        public void run() {
            if(tool.isNetworkAvailable(getApplicationContext()))
            parseResult(getList());
        }

        public String getList() {
            String baseURL = ((InfoApplication) getApplicationContext()).getBasehost() + "/sos/getonesos.do";
            List<NameValuePair> pair = new ArrayList<>();
            pair.add(new BasicNameValuePair("access_token", ((InfoApplication) getApplication()).getAccess_token()));
            pair.add(new BasicNameValuePair("status", "1"));
            for (NameValuePair pairs : pair) {
                System.out.println(pairs.getName() + " " + pairs.getValue());
            }
            return new HttpHelper().post(baseURL, pair);
        }
    }

    public boolean indanger = false;

    public void parseResult(String result) {
        try {
            JSONObject jsonObject = new JSONObject(result);
            switch (jsonObject.getInt("code")) {
                case 200:
                    if (jsonObject.getInt("result") != 0) {

                        showNotification();
                        indanger = true;
                    }

                    break;
                case 403:
                        Toast.makeText(PostTask.this, "Not Acceptable", Toast.LENGTH_SHORT).show();
                    break;
                case 406:
                     Toast.makeText(PostTask.this, "Forbidden", Toast.LENGTH_SHORT).show();
                    break;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (result == null) {
            //  Toast.makeText(getApplicationContext(), "请检查网络设置", Toast.LENGTH_SHORT).show();
            return;
        }

    }

    public class MyBinder extends Binder {

        public PostTask getService() {
            return PostTask.this;
        }
    }

    private MyBinder myBinder = new MyBinder();

    protected void showNotification() {
        CharSequence from = "警告";
        CharSequence message = "您的孩子有危险";

        Intent intent = new Intent(PostTask.this, MainActivity.class);


        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
        // construct the Notification object.
        Notification notif = new Notification(R.mipmap.ic_launcher, "您的孩子有危险",
                System.currentTimeMillis());
        notif.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_AUTO_CANCEL;
        notif.setLatestEventInfo(this, from, message, contentIntent);

        // look up the notification manager service
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(R.string.app_name, notif);
    }



}